package cn.peifu.hrm.service.impl;

import cn.peifu.hrm.constants.BaseConstants;
import cn.peifu.hrm.domain.Employee;
import cn.peifu.hrm.domain.Tenant;
import cn.peifu.hrm.exception.GlobalException;
import cn.peifu.hrm.exception.code.ErrorCode;
import cn.peifu.hrm.feign.LoginFeignClient;
import cn.peifu.hrm.mapper.EmployeeMapper;
import cn.peifu.hrm.mapper.TenantMapper;
import cn.peifu.hrm.service.ITenantService;
import cn.peifu.hrm.to.LoginMealTo;
import cn.peifu.hrm.to.Loginto;
import cn.peifu.hrm.to.TenantRegisterTo;
import cn.peifu.hrm.util.AjaxResult;
import cn.peifu.hrm.util.AssertUtils;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xie
 * @since 2021-08-15
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private LoginFeignClient loginFeignClient;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Value("${expired.days}")
    private int days;

    @Override
    @GlobalTransactional
    public void register(TenantRegisterTo to) {
        Tenant tenant = to.getTenant();
        Employee employee = to.getEmployee();
        String username = to.getUsername();
        String password = to.getPassword();
        Long mealId = to.getMealId();
        // 空校验
        // 合法性校验
        paramIsExist(tenant, employee,username);
        // 保存注册信息
        Loginto loginto = new Loginto();
        loginto.setUsername(username);
        loginto.setPassword(password);
        loginto.setType(BaseConstants.LoginConstants.TYPE_TENANT);
        // 获取保存信息的id
        AjaxResult result = loginFeignClient.save(loginto);
        // 空校验
        AssertUtils.isNull(result,ErrorCode.CODE_105_FAIL_PARAM.getMessage());
        Long LoginId = Long.valueOf(result.getResultObj().toString());
        // 保存入驻公司员工信息
        employee.setRealName(to.getUsername());
        employee.setType(BaseConstants.EmployeeConstants.STATE_NORMAL);
        Date now = new Date();
        employee.setInputTime(now);
        employee.setLoginId(LoginId);
        employee.setType(BaseConstants.PlatformTypeConstants.TYPE_PLATFORM_ADMIN);
        employeeMapper.insert(employee);
        // 保存入驻机构
        tenant.setAdminId(employee.getId());
        tenant.setRegisterTime(now);
        tenant.setState(BaseConstants.TenantConstants.STATE_AUDIT);
        super.insert(tenant);
        employee.setTenantId(tenant.getId());
        employeeMapper.updateById(employee);
        //保存购买的套餐
        LoginMealTo loginMealTo = new LoginMealTo();
        loginMealTo.setMealId(mealId);
        loginMealTo.setLoginId(LoginId);
        loginMealTo.setExpireDate(DateUtils.addDays(now,days));
        loginMealTo.setState(BaseConstants.MealStateConstants.STATE_TO_PAY);
        loginFeignClient.saveLoginMeal(loginMealTo);
    }

    @Override
    public Object selectByLoginId(Long loginId) {
        return tenantMapper.selectByLoginId(loginId);
    }

    /**
     * 校验参数是否为空、是否合法
     * @param tenant
     * @param employee
     * @param username
     */
    private void paramIsExist(Tenant tenant, Employee employee, String username) {
        AssertUtils.isNull(tenant.getCompanyName(), ErrorCode.CODE_100_NULL_COMPANYNAME.getMessage());
        AssertUtils.isNull(username,ErrorCode.CODE_101_NULL_USERNAME.getMessage());
        AssertUtils.isNotLicense(tenant.getCompanyNum(),ErrorCode.CODE_102_COMPANYNUM_ILLEGAL.getMessage());
        AssertUtils.isNotMobile(employee.getTel(),ErrorCode.CODE_103_FORMAT_INVALID_PHONE.getMessage());
        //判断是否已注册
        if(tenantMapper.checkTenantExist(tenant.getCompanyName())){
            throw new GlobalException(ErrorCode.CODE_104_EXIST_COMPANYNAME.getMessage());
        }
    }
}
